package top.wshape1.easyscan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import java.util.List;

import top.wshape1.easyscan.R;
import top.wshape1.easyscan.activity.EasyScanActivity;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-06-18
 */
public class CustomRuleSpinnerAdapter implements SpinnerAdapter {

    private List<ApplicationInfo> list;

    private EasyScanActivity mainActivity;

    public CustomRuleSpinnerAdapter(Context context, List<ApplicationInfo> list) {
        this.list = list;
        this.mainActivity = (EasyScanActivity) context;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(mainActivity, R.layout.list_view_custom_rule, null);

        ApplicationInfo applicationInfo = list.get(position);

        ((TextView) view.findViewById(R.id.tv_list_view_description)).setText(applicationInfo.loadLabel(mainActivity.getPackageManager()));
        ((TextView) view.findViewById(R.id.tv_list_view_string)).setText(applicationInfo.packageName);
        ((TextView) view.findViewById(R.id.tv_list_view_name)).setText("");
        ((ImageView) view.findViewById(R.id.iv_list_view_icon)).setImageDrawable(applicationInfo.loadIcon(mainActivity.getPackageManager()));

        return view;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @SuppressLint({"ViewHolder", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ApplicationInfo applicationInfo = list.get(position);

        TextView textView = new TextView(mainActivity);

        textView.setText(applicationInfo.loadLabel(mainActivity.getPackageManager()) + "[" + applicationInfo.packageName + "]");

        textView.setTextColor(mainActivity.getColor(R.color.black));

        textView.setTextSize(15);

        return textView;
    }

    @Override
    public int getItemViewType(int position) {
        return IGNORE_ITEM_VIEW_TYPE;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }
}
